package com.java.controller.admin;


import com.java.exceptioin.SrbException;
import com.java.pojo.entity.IntegralGrade;
import com.java.result.Result;
import com.java.result.ResultCodeEnum;
import com.java.service.IntegralGradeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 积分等级表 前端控制器
 * </p>
 *
 * @author Helen
 * @since 2021-07-08
 */
@Api("积分等级管理")
@RestController
@RequestMapping("/admin/core/integralGrade")
public class AdminIntegralGradeController {

    @Resource
    private IntegralGradeService integralGradeService;

    @ApiOperation("积分等级列表")
    @GetMapping("/list")
    public Result listAll(){
        List<IntegralGrade> list = integralGradeService.list();
        return Result.ok(list);

    }

    @ApiOperation(value = "根据id删除数据记录" ,notes = "逻辑删除数据")
    @DeleteMapping("/remove/{id}")
    public Result removeById(
            @ApiParam(value = "删除id")
            @PathVariable Long id){
        if(integralGradeService.removeById(id)){
            return Result.ok().message("删除成功");
        }else{
            return Result.fail().message("删除失败");
        }
    }

    @ApiOperation(value = "保存积分等级记录" ,notes = "保存积分等级记录")
    @PostMapping("/save")
    public Result save(@ApiParam(value = "等级积分") @RequestBody IntegralGrade integralGrade){

        if(integralGrade.getBorrowAmount() != null){
            boolean save = integralGradeService.save(integralGrade);
            return Result.ok().message("保存成功");
        }else{
            throw new SrbException(ResultCodeEnum.BORROW_AMOUNT_NULL_ERROR);//额度不能为空
        }
    }

    @ApiOperation(value = "根据Id获取积分等级数据记录")
    @GetMapping("/get/{id}")
    public Result getById(
            @ApiParam(value = "等级积分id")
            @PathVariable("id") Long id){
        IntegralGrade integralGrade = integralGradeService.getById(id);
        return Result.ok(integralGrade);
    }


    @ApiOperation(value = "更新积分等级")
    @PutMapping("/update")
    public Result updateById(@RequestBody IntegralGrade integralGrade){
        boolean update = integralGradeService.updateById(integralGrade);
        if(update){
            return Result.ok().message("更新成功");
        }else{
            return Result.ok().message("更新失败");
        }
    }
}

